package com.huangzhao.gulimall.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author huangzhao
 */
public class ListValuesConstraintValidator implements ConstraintValidator<ListValues, Integer> {

    private Set<Integer> valueSet = new HashSet<>();

    @Override
    public void initialize(ListValues constraintAnnotation) {
        int[] values = constraintAnnotation.value();
        for(int val : values){
            valueSet.add(val);
        }
    }

    /**
     * 判断是否校验成功
     *
     * @param value
     * @param context
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        boolean contain = valueSet.contains(value);
        return contain;
    }
}
